import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JSplitPane;
import javax.swing.SwingConstants;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;


public class DialogRejoindreSalon extends JDialog {
	private JPasswordField passwordField;
	private Client controleur;
	private JList list;
	private JLabel lblMotDePasse;
	
	private String[] salons;
	
	public DialogRejoindreSalon(Client client) throws SQLException, ClassNotFoundException {
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				fermerFenetre();
			}
		});
		setModal(true);
		this.controleur = client;
		this.setBounds(150,150,450,317);
		getContentPane().setLayout(null);
		
		salons = Connexion_BD.getSalons();
		
		this.list = new JList(salons);
		list.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				try {
					GererChampMdp();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (ClassNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		list.setBounds(12, 12, 418, 158);
		getContentPane().add(list);
		
		JLabel label = new JLabel("Liste des salons");
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setBounds(12, 182, 418, 15);
		getContentPane().add(label);
		
		JSplitPane splitPane = new JSplitPane();
		splitPane.setBounds(178, 251, 252, 27);
		getContentPane().add(splitPane);
		
		JButton btnFermer = new JButton("Fermer");
		btnFermer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				fermerFenetre();
			}
		});
		splitPane.setLeftComponent(btnFermer);
		
		JButton btnRejoindre = new JButton("Rejoindre");
		btnRejoindre.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					rejoindreSalon();
				} catch (HeadlessException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (ClassNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		splitPane.setRightComponent(btnRejoindre);
		
		lblMotDePasse = new JLabel("Mot de passe");
		lblMotDePasse.setEnabled(false);
		lblMotDePasse.setHorizontalAlignment(SwingConstants.RIGHT);
		lblMotDePasse.setBounds(12, 220, 117, 15);
		getContentPane().add(lblMotDePasse);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(147, 218, 282, 19);
		getContentPane().add(passwordField);
	}
	
	public void fermerFenetre()
	{
		this.controleur.fermerDialogRejoindreSalon();
	}
	
	public void activationPublic()
	{
		lblMotDePasse.setEnabled(false);
		passwordField.setEnabled(false);
		passwordField.setText("");
	}
	
	public void activationPrive()
	{
		lblMotDePasse.setEnabled(true);
		passwordField.setEnabled(true);
	}
	
	private void GererChampMdp() throws SQLException, ClassNotFoundException {
		int num = list.getSelectedIndex();
		String salon_a_supprimer = this.salons[num];
		
		if (! Connexion_BD.estPublic(salon_a_supprimer)) 
		{
			this.activationPrive();
		}
		else if (Connexion_BD.estPublic(salon_a_supprimer))
		{
			this.activationPublic();
		}
		
	}
	
	private void rejoindreSalon() throws HeadlessException, SQLException, ClassNotFoundException {
		int num = list.getSelectedIndex();
		String salon_a_rejoindre = null;
		String bon_mdp = null;
		String password = new String(passwordField.getPassword());
		
		salon_a_rejoindre = this.salons[num];
		
		bon_mdp = Connexion_BD.getMdpSalon(salon_a_rejoindre);


		if (! Connexion_BD.estPublic(salon_a_rejoindre) && password.equals("")) 
		{
			JOptionPane.showMessageDialog(this, "Vous devez entrer le mot de passe du salon !", "Erreur : Mot de passe manquant", JOptionPane.ERROR_MESSAGE);
		}
		
		else if (! Connexion_BD.estPublic(salon_a_rejoindre) && ! password.equals(bon_mdp))
		{
			JOptionPane.showMessageDialog(this, "Le mot de passe entré pour ce salon est incorrect !", "Erreur : Mot de passe incorrect", JOptionPane.ERROR_MESSAGE);
		}
		
		else if (this.controleur.rejoindreSalon(salon_a_rejoindre, password)) 
		{
			JOptionPane.showMessageDialog(this, "Vous avez rejoins le salon  " + salon_a_rejoindre + " ", "Confirmation salon joint", JOptionPane.INFORMATION_MESSAGE);
		}
		
	}
}
